﻿// NovoSerDes_Eva.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include "INovoComm.h"
#include <wtypes.h>

#define DYNAMIC_DLL

#ifndef DYNAMIC_DLL
#pragma comment(lib, "../Output/NovoComm.lib")
#endif

#define GPIO(x) (0x01 << x)

int main()
{
    std::cout << "Hello World!\n";

    int DevTypeID = 0;
    DEV_CONFIG myDev;
    myDev.devType = CH347;
    myDev.I2C_EN = true;
    myDev.SPI_EN = false;
    myDev.UART_EN = false;
    myDev.GPIO_EN = false;
    
    int ret = 0;
    INOVO_COMM* novoComm = NULL;
    HINSTANCE hinstLib = NULL;
    typedef INOVO_COMM* (*sdkif_create_Fun)();
    typedef void(*sdkif_destroy_Fun)(INOVO_COMM* pSdkIF);
    sdkif_create_Fun sdkif_create_dll = NULL;
    sdkif_destroy_Fun sdkif_destroy_dll = NULL;
    
    if (hinstLib == NULL)
    hinstLib = LoadLibraryEx(TEXT("..//Output//NovoComm.dll"), NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
    
    if (hinstLib != NULL)
    {
        sdkif_create_dll = (sdkif_create_Fun)GetProcAddress(hinstLib, "sdkif_create");
        sdkif_destroy_dll = (sdkif_destroy_Fun)GetProcAddress(hinstLib, "sdkif_destroy");
        novoComm = sdkif_create_dll();
    }
    
    /* General APIs*/
#define GENERAL_API_TEST_EN
#ifdef GENERAL_API_TEST_EN
    ret = novoComm->Novo_InitDev(&myDev);
    printf("Novo_InitDev result = %d\n", ret);
    
    char devName[10];		
    novoComm->Novo_GetDevName(&devName[0]);
    printf("Dev Name = %s\n", devName);
    
    ret = novoComm->Novo_SetDeviceId(2);
    ret = novoComm->Novo_SetDeviceId(0);

    ret = novoComm->Novo_EnumDevice();
        
    if (!novoComm->Novo_OpenDevice())
    {
        printf("Open Device success!\n");
        if (!novoComm->Novo_CloseDevice())
            printf("Close Device success!\n");
    }
#endif

    /* I2C APIs */
#define IIC_TEST_EN
#ifdef IIC_TEST_EN
    if (!novoComm->Novo_OpenDevice())
    {
        printf("Open Device success!\n");
    }

    IIC_CONFIG i2c_cfg;
    i2c_cfg.ClockSpeedHz = IIC_100K;
    i2c_cfg.EnablePu = FALSE;
    ret = novoComm->Novo_IIC_Init(&i2c_cfg);
    printf("IIC Init: %d \n", ret);
    
    ret = novoComm->Novo_IIC_SetIndex(1);
    printf("Set IIC index: %d \n", ret);
    
    UCHAR devAddrList[128] = { 0 };
    ret = novoComm->Novo_IIC_ScanDevAddr(&devAddrList[0]);
    printf("Novo_IIC_ScanDevAddr count: %d \n", ret);

    UCHAR devAddr_Max96712 = 0x52;
    USHORT testRegAddr = 0x08A3;
    UCHAR testRegData_wr = 0xE4;
    UCHAR testRegData_rd[4096] = {0};
    ret = novoComm->Novo_IIC_Rd_U16U8(devAddr_Max96712, testRegAddr, &testRegData_rd[0]);
    printf("Read Reg0x{%x}: %x \n", testRegAddr, testRegData_rd[0]);
    ret = novoComm->Novo_IIC_Wr_U16U8(devAddr_Max96712, testRegAddr, testRegData_wr);
    ret = novoComm->Novo_IIC_Rd_U16U8(devAddr_Max96712, testRegAddr, &testRegData_rd[0]);
    printf("Read Reg0x{%x}: %x \n", testRegAddr, testRegData_rd[0]);

    USHORT* ptempRdData = (USHORT*)&testRegData_rd[0];
    ret = novoComm->Novo_IIC_Wr_U16(devAddr_Max96712, testRegAddr, 0x1122);
    ret = novoComm->Novo_IIC_Rd_U16(devAddr_Max96712, testRegAddr, ptempRdData);
    printf("Read Reg0x{%x}: %x \n", testRegAddr, ptempRdData[0]);

    UINT32* ptempRdData_U32 = (UINT32*)&testRegData_rd[0];
    ret = novoComm->Novo_IIC_Wr_U16U32(devAddr_Max96712, testRegAddr, 0x11223344);
    ret = novoComm->Novo_IIC_Rd_U16U32(devAddr_Max96712, testRegAddr, ptempRdData_U32);
    printf("Read Reg0x{%x}: %x \n", testRegAddr, ptempRdData_U32[0]);

    if (!novoComm->Novo_CloseDevice())
        printf("Close Device success!\n");

#endif
        
#define GPIO_TEST_EN
#ifdef GPIO_TEST_EN
    if (!novoComm->Novo_OpenDevice())
    {
        printf("Open Device success!\n");
    }

    UCHAR gpio_data = 0;
    UCHAR gpio_ix = 5;
    if (!novoComm->Novo_GPIO_Get((UCHAR)GPIO(gpio_ix), &gpio_data))
        printf("Get GPIO_%d: %x\n", gpio_ix, gpio_data);

    UCHAR gpio_set = 0x00;
    if(!novoComm->Novo_GPIO_Set((UCHAR)GPIO(gpio_ix), gpio_set))
        printf("Set GPIO_%d: %x\n", gpio_ix, gpio_set);

    if (!novoComm->Novo_GPIO_Get((UCHAR)GPIO(gpio_ix), &gpio_data))
        printf("Get GPIO_%d: %x\n", gpio_ix, gpio_data);

    if (!novoComm->Novo_CloseDevice())
        printf("Close Device success!\n");

#endif



}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
